using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using DTO;

namespace DAL
{
    public class DieuKienSuDung_DAL
    {
        private SqlConnection cn = null;
        public  DieuKienSuDung_DAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(DieuKienSuDung_DTO dieuKienSuDung)
        {
            SqlCommand cm = new SqlCommand("tb_DieuKienSuDung_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            //cm.Parameters.AddWithValue("@ID", dieuKienSuDung.ID);
            cm.Parameters.AddWithValue("@MaMH", dieuKienSuDung.MaMH);
            cm.Parameters.AddWithValue("@DIEUKIENSD", dieuKienSuDung.DieuKienSuDung);
            cm.Parameters.AddWithValue("@LoiGioiThieu", dieuKienSuDung.LoiGioiThieu);
            cm.Parameters.AddWithValue("@DacDiemNoiBat", dieuKienSuDung.DacDiemNoiBat);
            cm.Parameters.AddWithValue("@GHICHU", dieuKienSuDung.GhiChu);

            cm.ExecuteNonQuery();
        }
        public void Update(DieuKienSuDung_DTO dieuKienSuDung)
        {
            SqlCommand cm = new SqlCommand("tb_DieuKienSuDung_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@ID", dieuKienSuDung.ID);
            cm.Parameters.AddWithValue("@MaMH", dieuKienSuDung.MaMH);
            cm.Parameters.AddWithValue("@DIEUKIENSD", dieuKienSuDung.DieuKienSuDung);
            cm.Parameters.AddWithValue("@LoiGioiThieu", dieuKienSuDung.LoiGioiThieu);
            cm.Parameters.AddWithValue("@DacDiemNoiBat", dieuKienSuDung.DacDiemNoiBat);
            cm.Parameters.AddWithValue("@GHICHU", dieuKienSuDung.GhiChu);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("tb_DieuKienSuDung_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@ID", iD);
            cm.ExecuteNonQuery();
        }
        public tbDieuKienSuDungCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("tb_DieuKienSuDung_GET", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbDieuKienSuDungCollection dieuKienSuDungCollection = new tbDieuKienSuDungCollection();

            while (dr.Read())
            {
                DieuKienSuDung_DTO dieuKienSuDung = new DieuKienSuDung_DTO();
                dieuKienSuDung.ID = int.Parse(dr.GetValue(0).ToString());
                dieuKienSuDung.MaMH = int.Parse (dr.GetValue(1).ToString());
                dieuKienSuDung.DieuKienSuDung = dr.GetValue(2).ToString();
                dieuKienSuDung.LoiGioiThieu = dr.GetValue(3).ToString();
                dieuKienSuDung.DacDiemNoiBat = dr.GetValue(4).ToString();
                dieuKienSuDung.GhiChu = dr.GetValue(5).ToString();
                dieuKienSuDungCollection.Add(dieuKienSuDung);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return dieuKienSuDungCollection;
        }
    }
}
